function [ partitionQuality ] = place_then_sched( dataset, k, m )
% Function to implement the placement and scheduling of the nodes using a brute-force approach
% 1) choose A to maximize (2) given 
% 2) choose a partition of A = A1,...,AT to solve (5) and (6)
% Notes:
%   - dataset is an array containing node numbers in first row, sensor measurements for different times in the rows
%   - notPlaced is a list of the node numbers that have not been placed, these should be matched to row 1 of dataset

    % This matrix stores all possible times, and what sensors are in them
    % Zeros represent empty slots. Since this is matlab, the matrix is big
    ats = zeros(k, m);
    
    % array of sensor numbers that have yet to be placed
    % will gradually move columns from notPlaced to placed matrix
    notPlaced = dataset;
    placed = [];

    % place m sensors
    for i=1:m
        % start by getting F(A) function for current set A
        initQuality = sensingQuality(placed(2:end,:),notPlaced(2:end,:));
        
        % iterate through all sensors to check which gives biggest F(A)
        delta = [];
        for j=1:size(notPlaced,2)
            
            % temporarily move a sensor from notPlaced over to placed
            tmpSet = [placed notPlaced(:,j)];
            tmpSetBar = notPlaced;
            tmpSetBar(:,j) = [];

            % compute the quality with this new sensor data included, exclude first row b.c. it contains sensor id's
            delta(j) = sensingQuality(tmpSet(2:end,:),tmpSetBar(2:end,:)) - initQuality;
        end

        % add element with highest delta to the set (raises the sensingQuality by delta)
        [junk,index] = max(delta);
        placed = [placed notPlaced(:,index)];
        notPlaced(:,index) = [];
    end
    
    % schedule sensors into k time slots
    % maximize:
    % 1) max (1/#As in partition) * sum (over all As in that partition) F(At)
    % 2) max (over all partitions) min F(At)

    % track the partition with the lowest F(A), will be adding this node to that partition here
    partitionQuality = zeros(k,1);
    for i=1:m
        
        % find partition with lowest current quality
        [junk,k] = min(partitionQuality);
            
        % grow the set for this partition
        tmpSetBar = dataset;
        tmpSet = [];
        n=1;
        while n<=size(tmpSetBar,2)
            % delete the node data from tmpSetBar if it's node ID shows up in the current partition
            if ismember(tmpSetBar(1,n),ats(k,:))
                tmpSet = [tmpSet tmpSetBar(:,n)];
                tmpSetBar(:,n) = [];
            else
                n = n + 1;
            end
        end

        % try adding all nodes to this partition and see which does the best
        delta = [];
        for j=1:size(placed,2)
        
            % temporarily move a sensor from placed over to scheduled
            tmpSet = [tmpSet placed(:,j)];
            for n=1:size(tmpSetBar,2)
                if placed(1,j)==tmpSetBar(1,n)
                    tmpSetBar(:,n) = [];
                    break;
                end
            end

            % compute the quality with this new sensor data included, exclude first row b.c. it contains sensor id's
            delta(j) = sensingQuality(tmpSet(2:end,:),tmpSetBar(2:end,:));

            % move the entry back to the set of nodes not accounted for
            tmpSetBar = [tmpSetBar tmpSet(:,end)];
            tmpSet(:,end) = [];
        end

        % add the node that did best to the partition
        [junk,index] = max(delta);
        ats(k(1),i) = placed(1,index);
        placed(:,index) = [];
        partitionQuality(k) = delta(index);
    end
end
